/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services.ie.com.stamply.geo

import iorder.Address

/**
 *
 * @author Fabiano
 */
class GeoCoder {
        
    String base = 'http://maps.google.com/maps/api/geocode/xml?'
   
    void fillInLatLng(Address address) {
        
        String urlEncodedAddress =
                [address.address1, address.city, address.country].collect {
                    URLEncoder.encode(it,'UTF-8')
                }.join(',+')
        String url = base + [sensor:false, address:urlEncodedAddress].collect { it }.join('&')
        def response = new XmlSlurper().parse(url)
        address.latitude = response.result.geometry.location.lat[0] ?: "0.0"
        address.longitude = response.result.geometry.location.lng[0] ?: "0.0"
    }
}

	
